<?php
/**
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Celtawebtickets component helper.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_celtawebtickets
 * @since		1.6
 */
class CeltawebticketsHelper
{
	/**
	 * Configure the Linkbar.
	 *
	 * @param	string	The name of the active view.
	 *
	 * @return	void
	 * @since	1.6
	 */
	public static function addSubmenu($vName)
	{
                JSubMenuHelper::addEntry(
			JText::_('COM_CELTAWEBTICKETS_SUBMENU_CPANEL'),
			'index.php?option=com_celtawebtickets&view=controlpanel',
			$vName == 'controlpanel'
		);
		JSubMenuHelper::addEntry(
			JText::_('COM_CELTAWEBTICKETS_SUBMENU_TICKETS'),
			'index.php?option=com_celtawebtickets&view=tickets',
			$vName == 'tickets'
		);

		JSubMenuHelper::addEntry(
			JText::_('COM_CELTAWEBTICKETS_SUBMENU_CATEGORIES'),
			'index.php?option=com_categories&extension=com_celtawebtickets',
			$vName == 'categories'
		);

		JSubMenuHelper::addEntry(
			JText::_('COM_CELTAWEBTICKETS_SUBMENU_USERS'),
			'index.php?option=com_celtawebtickets&view=users',
			$vName == 'users'
		);
                
                JSubMenuHelper::addEntry(
			JText::_('COM_CELTAWEBTICKETS_SUBMENU_MAIL'),
			'index.php?option=com_celtawebtickets&view=mailtemplates',
			$vName == 'mailtemplates'
		);
                
                if ($vName=='categories') {
			JToolBarHelper::title(
				JText::sprintf('COM_CELTAWEBTICKETS_SUBMENU_CATEGORIES', JText::_('com_celtawebtickets')),
				'celtawebtickets-categories');
		}


	}

	/**
	 * Gets a list of the actions that can be performed.
	 *
	 * @param	int		The category ID.
	 *
	 * @return	JObject
	 * @since	1.6
	 */
	public static function getActions($categoryId = 0)
	{
		$user	= JFactory::getUser();
		$result	= new JObject;

		if (empty($categoryId)) {
			$assetName = 'com_celtawebtickets';
		} else {
			$assetName = 'com_celtawebtickets.category.'.(int) $categoryId;
		}

		$actions = array(
			'core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.state', 'core.delete'
		);

		foreach ($actions as $action) {
			$result->set($action,	$user->authorise($action, $assetName));
		}

		return $result;
	}


	public static function getUserOptions()
	{
		// Initialize variables.
		$options = array();

		$db	= JFactory::getDbo();
		$query	= $db->getQuery(true);

		$query->select('a.id As value, a.name As text');
		$query->from('#__users AS a');
                
                                // Join over the users for the checked out user.
		$query->join('LEFT',  $db->quoteName('#__cwtickets_users').' AS um ON um.user_id = a.id');
		$query->order('a.name');
                $query->where('um.isuser = 1 AND um.state = 1');

		// Get the options.
		$db->setQuery($query);

		$options = $db->loadObjectList();

		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}

		// Merge any additional options in the XML definition.
		//$options = array_merge(parent::getOptions(), $options);

		array_unshift($options, JHtml::_('select.option', '', JText::_('COM_CELTAWEBTICKETS_NO_USER')));

		return $options;
	}
        
        public static function getRepOptions()
	{
		// Initialize variables.
		$options = array();

		$db	= JFactory::getDbo();
		$query	= $db->getQuery(true);

		$query->select('a.id As value, a.name As text');
		$query->from('#__users AS a');
                
                                // Join over the users for the checked out user.
		$query->join('LEFT',  $db->quoteName('#__cwtickets_users').' AS um ON um.user_id = a.id');
		$query->order('a.name');
                $query->where('um.isrep = 1 AND um.state = 1');

		// Get the options.
		$db->setQuery($query);

		$options = $db->loadObjectList();

		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}

		// Merge any additional options in the XML definition.
		//$options = array_merge(parent::getOptions(), $options);

		array_unshift($options, JHtml::_('select.option', '0', JText::_('COM_CELTAWEBTICKETS_NO_USER')));

		return $options;
	}
        
        
	/**
	 * A helper to get the list of user groups.
	 *
	 * @return	array
	 * @since	1.6
	 */
	protected function getUserGroups()
	{
		// Get a database object.
		$db = JFactory::getDBO();

		// Get the user groups from the database.
		$db->setQuery(
			'SELECT a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level' .
			' FROM #__usergroups AS a' .
			' LEFT JOIN `#__usergroups` AS b ON a.lft > b.lft AND a.rgt < b.rgt' .
			' GROUP BY a.id' .
			' ORDER BY a.lft ASC'
		);
		$options = $db->loadObjectList();

		return $options;
	}
}
